package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.juzi.wopu.domain.OrderJingCai;
import com.juzi.wopu.domain.OrderJingCaiResponse;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.query.OrderJingCaiQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.service.IOrderJingCaiService;
import com.juzi.wopu.service.ISysCodeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class OrderJingCaiAction extends CRUDAaction<OrderJingCai> {
	
	private static final long serialVersionUID = 1L;

	private IOrderJingCaiService orderJingCaiService;
	
	private ISysCodeService sysCodeService;

	private OrderJingCai orderJingCai = new OrderJingCai();
	
	private OrderJingCaiQuery baseQuery = new OrderJingCaiQuery();
	
	private PageList pageList;
	
	private List<OrderJingCaiResponse> data=new ArrayList<>();
	
	private String totalMoney;
	
	private String totalOrder;
	
	public void setOrderJingCaiService(IOrderJingCaiService orderJingCaiService) {
		this.orderJingCaiService = orderJingCaiService;
	}
	
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	@Override
	protected void list() {
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		Map<String, SysCode> syscodeMap=new HashMap<>();
		Map<Long, SysCode> map1=new HashMap<>();
		sysCodeService.getSysCodeMap(map1, syscodeMap);
		long companyCode=syscodeMap.get(companyName).getiId();
		baseQuery.setCompanyCode(companyCode); 
		this.pageList = orderJingCaiService.findByQuery(baseQuery);
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			totalOrder=pageList.getTotalcount()+"";
			double money=orderJingCaiService.getTotalCount(baseQuery);
			totalMoney=money+"";
			for(int i=0;i<pageList.getRows().size();i++){
				OrderJingCai orderJingCai=(OrderJingCai) pageList.getRows().get(i);
				OrderJingCaiResponse response=new OrderJingCaiResponse(orderJingCai);
				Long iCompany = orderJingCai.getiCompany();
				SysCode sysCode = map1.get(iCompany);
				if(iCompany==null){
					response.setCompanyName("");
				}else{
					String sName = sysCode.getsName();
					response.setCompanyName(sName);
				}
				data.add(response);
			}
		}else {
			totalMoney="0.0";
			totalOrder="0";
		}
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	public void validateSave() {

	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				orderJingCaiService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {
			orderJingCai = orderJingCaiService.get(id);
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			orderJingCai = new OrderJingCai();
		} else {
			orderJingCai = orderJingCaiService.get(id);
		}
	}

	@Override
	public OrderJingCai getModel() {
		return orderJingCai;
	}

	public PageList getPageList() {
		return pageList;
	}

	public OrderJingCaiQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(OrderJingCaiQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public List<OrderJingCaiResponse> getData() {
		return data;
	}

	public void setData(List<OrderJingCaiResponse> data) {
		this.data = data;
	}

	public String getTotalMoney() {
		return totalMoney;
	}

	public void setTotalMoney(String totalMoney) {
		this.totalMoney = totalMoney;
	}

	public String getTotalOrder() {
		return totalOrder;
	}

	public void setTotalOrder(String totalOrder) {
		this.totalOrder = totalOrder;
	}
	
}
